<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class mcategory extends MY_Model {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );
    public $mytable = "category";
    public $primaryKey = "cat_id";
    public $resCode = "CATEGORY";

    function __construct() {
        parent::__construct();
    }

    function field() {
		$fieldData = array();
	$fieldData['cat_name'] = '';
	$fieldData['cat_name_ascii'] = '';
	$fieldData['cat_code'] = '';
	$fieldData['cat_description'] = '';
	$fieldData['cat_parent'] = 0;
	$fieldData['cat_count'] = 0;
	$fieldData['cat_order'] = 0;
	$fieldData['cat_status'] = 0;
	$fieldData['seo_h1'] = '';
	$fieldData['seo_description'] = '';
	$fieldData['seo_title'] = '';
	$fieldData['seo_keyword'] = '';
	$fieldData['cat_icon'] = '';
	$fieldData['cat_time_update'] = time();
	$fieldData['cat_time_create'] = time();
	return $fieldData;
    }
	
	public function get_cat_tree(){
		$cate_tree= array();
		$cate_sub= array();
		$categoryParent = $this->gets(array('CAT_PARENT'=>0));
		$categorySub = $this->gets(array('CAT_PARENT > '=>0));
		if($categorySub){
			foreach($categorySub as $sub){
				$cate_sub[$sub->cat_parent][$sub->cat_id] = '- '.$sub->cat_name;
			}
		}
		if ($categoryParent) {
			for($i=0; $i<count($categoryParent); $i++) {
				$cate_tree[$categoryParent[$i]->cat_id] = $categoryParent[$i]->cat_name;
				if(isset($cate_sub[$categoryParent[$i]->cat_id])){
					foreach($cate_sub[$categoryParent[$i]->cat_id] as $k=>$v){
						$cate_tree[$k] = $v;
					}
				}
			}
		}
		
		return $cate_tree;
	}
	
	public function get_array_slug() {
		$cates = $this->gets();
		$cates = $this->key_value($cates, 'cat_code');
		return $cates;
	}
	
	public function get_cat_tree_array($published = false){
		$cate_sub= array();
		$this->db->order_by('cat_order asc');
		if ($published) 
			$this->db->where('cat_status > ', 0);
		$categoryParent = $this->gets(array('CAT_PARENT'=>0));
		$this->db->order_by('cat_order asc');
		if ($published) 
			$this->db->where('cat_status > ', 0);
		$categorySub = $this->gets(array('CAT_PARENT > '=>0));
		if ($categoryParent) {
			foreach ($categoryParent as $parent) {
				$cate_sub[$parent->cat_id]['parent'] = $parent;
			}
		}
		if($categorySub){
			foreach($categorySub as $sub){
				$cate_sub[$sub->cat_parent]['child'][$sub->cat_id] = $sub;
			}
		} 
		
		return $cate_sub;
	}
	
}

?>